package org.starter;

import org.springframework.context.annotation.Bean;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import com.easyaop.api.Arounds;
import com.easyaop.api.advisor.AnnotationPointcut;
import com.easyaop.api.advisor.Pointcut;
import com.easyaop.api.spring.FromMethodInterceptor;
import com.easyaop.core.AroundsImpl;

public class EasyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {

	@Override
	public void setImportMetadata(AnnotationMetadata importMetadata) {
		// System.out.println("setImportMetadata");

	}

	@Bean
	public TransactionInterceptor transactionInterceptor() {
		TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource(false);
		TransactionInterceptor interceptor = new TransactionInterceptor();
		interceptor.setTransactionAttributeSource(transactionAttributeSource);
		Arounds arounds = AroundsImpl.getInstance();
		// todo 写成动态
		Pointcut pointcut = new AnnotationPointcut("org.my", Transactional.class);
		// arounds.add(pointcut, (target, args, join, method) -> {
		// 	System.out.println("tx starting");
		// 	return method.call();
		// });
		System.out.println("init aop");
		FromMethodInterceptor fromMethodInterceptor = new FromMethodInterceptor(interceptor);
		arounds.add(pointcut, fromMethodInterceptor);
		return interceptor;
	}

}
